package com.jiandaoshou;

import android.os.Handler;
import android.os.Message;
import android.widget.ListView;
import android.widget.TextView;

import com.jiandaoshou.adapter.OrderAdapter;
import com.jiandaoshou.base.BaseActivity;
import com.jiandaoshou.entry.OrderDetailInfo;
import com.jiandaoshou.http.APIDefine;
import com.jiandaoshou.http.Http;
import com.jiandaoshou.utils.JsonUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class JinRiDingDetailAct extends BaseActivity {
    private String strOrderID;
    private String strPayMode;
    private String strCount;
    private String strTime;
    //订单号
    private TextView Tv_OrderID;
    //订单价格
    private TextView Tv_OrderPrice;
    //支付方式
    private TextView Tv_OrderPayMode;
    //商品序号
    private TextView Tv_ShopNum;
    //商品名称
    private TextView Tv_ShopName;
    //订单量
    private TextView Tv_OrderNum;
    //订单价格
    private TextView Tv_ShopPirce;
    //订单支付方式
    private TextView Tv_shopPayMode;
    //订单下单时间
    private TextView Tv_DingDanTime;

    private ListView lvOrderList;

    private List<OrderDetailInfo> mOrders = new ArrayList<>();
    private OrderAdapter orderAdapter;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            List<OrderDetailInfo> Infos = (List<OrderDetailInfo>) msg.obj;
            switch (msg.what) {
                case 0:
                    mOrders = Infos;
                    orderAdapter.setList(mOrders);

                    break;
            }
        }
    };

    @Override
    public boolean showBackLogo() {
        return true;
    }

    @Override
    protected int getContentViewRes() {
        return R.layout.activity_jinriding_detail;
    }

    @Override
    protected String getTitleText() {
        return "订单详情";
    }

    @Override
    public void init() {
        getToolbar().setNavigationIcon(R.drawable.selector_tab_btn);
        //获取订单的ID
        strOrderID = getIntent().getStringExtra("OrderID");
        strPayMode = getIntent().getStringExtra("payMode");
        strCount = getIntent().getStringExtra("aount");
        strTime = getIntent().getStringExtra("time");
        initId();

        if (strPayMode.equals("1")) {
            Tv_shopPayMode.setText("微信支付");
            Tv_OrderPayMode.setText("微信支付");
        } else if (strPayMode.equals("2")) {
            Tv_shopPayMode.setText("支付宝支付");
            Tv_OrderPayMode.setText("支付宝支付");
        }

        Tv_DingDanTime.setText("日期:" + strTime);
        Tv_OrderID.setText(strOrderID);
        Tv_OrderPrice.setText(strCount);

        //获取数据
        HashMap<String, String> map = new HashMap<>();
        map.put("strOrderID", strOrderID);
        Http.post(APIDefine.TF_Interface_GetOrderDetailMsg, map, new Http.HttpCallBack() {
            @Override
            public void onHttpSuccess(String response) throws Exception {
                JSONObject object = new JSONObject(response);
                JSONArray array = (JSONArray) object.get("OrderDetailList");
                OrderDetailInfo Info = null;
                for (int i = 0; i < array.length(); i++) {
                    Info = JsonUtil.parser.fromJson(array.get(i).toString(), OrderDetailInfo.class);
                    mOrders.add(Info);
                }
                Message msg = handler.obtainMessage();
                msg.obj = mOrders;
                msg.what = 0;
                msg.sendToTarget();
            }
        });
    }

    private void initId() {
        Tv_OrderID = $(R.id.detail_orderid);
        Tv_OrderPrice = $(R.id.detail_price);
        Tv_OrderPayMode = $(R.id.detail_paymode);
        Tv_ShopNum = $(R.id.detail_warenum);
        Tv_ShopName = $(R.id.detail_name);
        Tv_OrderNum = $(R.id.detail_ordernum);
        Tv_ShopPirce = $(R.id.detail_orderprice);
        Tv_shopPayMode = $(R.id.detail_orderpaymode);
        Tv_DingDanTime = $(R.id.detail_ordertime);
        lvOrderList = $(R.id.orderList);

        orderAdapter = new OrderAdapter(this, mOrders);
        lvOrderList.setAdapter(orderAdapter);
    }
}
